# 导入 csv 库
import csv

# 以读方式打开文件
with open("../data/m-a_edges.csv", mode="r", encoding="utf-8-sig") as f:
    
    # 基于打开的文件，创建csv.reader实例
    reader = list(csv.reader(f))
    # print(reader[945])
    # 获取第一行的header
    # header[0] = "source"
    # header[1] = "target"
    header = reader[0][0].split('\t')
    reader.pop(0)
    # 逐行获取数据，并输出
    #row[0] -> mashup; row[1] -> api
    dic = {}
    for rows in reader:
        if len(rows) != 1:
            temp = ''
            for i in rows:
                temp += i
            for i in range(1,len(rows),-1):
                del rows[i]
            rows[0] = temp
        row = rows[0].split('\t')
        # print(row)
        if row[0] in dic:
            dic[row[0]].add(row[1])
        else:
            dic[row[0]] = set()
            dic[row[0]].add(row[1])

# 以读方式打开文件
with open("../data/mashup_nodes_estimator.csv", mode="r", encoding="utf-8-sig") as f:
    
    # 基于打开的文件，创建csv.reader实例
    reader = list(csv.reader(f))
    # print(reader[945])
    # 获取第一行的header
    # header[0] = "source"
    # header[1] = "target"
    header = reader[0][0].split('\t')
    reader.pop(0)
    # 逐行获取数据，并输出
    #row[0] -> mashup; row[1] -> api
    category_mushup = {}
    for rows in reader:
        if len(rows) != 1:
            temp = ''
            for i in rows:
                temp += i
            for i in range(1,len(rows),-1):
                del rows[i]
            rows[0] = temp
        row = rows[0].split('\t')
        # print(row)
        if row[6] in category_mushup:
            category_mushup[row[6]].add(row[2])
        else:
            category_mushup[row[6]] = set()
            category_mushup[row[6]].add(row[2])
    # print(len(category_mushup)) 337      

# 以读方式打开文件
with open("../data/api_nodes_estimator.csv", mode="r", encoding="utf-8-sig") as f:
    
    # 基于打开的文件，创建csv.reader实例
    reader = list(csv.reader(f))
    # print(reader[945])
    # 获取第一行的header
    # header[0] = "source"
    # header[1] = "target"
    header = reader[0][0].split('\t')
    reader.pop(0)
    # 逐行获取数据，并输出
    #row[0] -> mashup; row[1] -> api
    category_api = {}
    for rows in reader:
        if len(rows) != 1:
            temp = ''
            for i in rows:
                temp += i
            for i in range(1,len(rows),-1):
                del rows[i]
            rows[0] = temp
        row = rows[0].split('\t')
        # print(row)
        if row[6] in category_api:
            category_api[row[6]].add(row[1])
        else:
            category_api[row[6]] = set()
            category_api[row[6]].add(row[1])
    # print(len(category_api))  428


for cate in category_mushup: ##对每个类别做一个表格
    if cate in category_api: ##api的字典中有没有这个类别
        output = {} ##每个类别的表格先用字典表示
        for i in category_mushup[cate]: ##这个类别中的每一个mashup
            if i in dic: ##如果不在dic中就无法进行mashup与api的对应，应该排除
                for j in category_api[cate]: ##j是每一个api
                    # print(j)
                    if j in dic[i]: ##在问题一的表格中找对应
                        if i in output:
                            output[i].add(j)
                        else:
                            output[i] = set()  
                            output[i].add(j)  ##写到输出字典中
        # print(output)
        if output != {}:
            # 以写方式打开文件。注意添加 newline=""，否则会在两行数据之间都插入一行空白。
            with open("../output/output2/"+cate+".csv", mode="w", encoding="utf-8-sig", newline="") as f:                
                # 基于打开的文件，创建 csv.writer 实例
                writer = csv.writer(f)

                # 写入 header。
                # writerow() 一次只能写入一行。
                writer.writerow(['Mashup','API'])

                for out in output:
                    writer.writerow([out]+list(output[out]))                